package helipay.api.web.controller.webservice;

import helipay.api.common.exception.BaseAPIException;
import helipay.api.model.Merchant;
import helipay.api.service.MerchantService;
import helipay.api.util.I18nUtil;
import helipay.api.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


@Controller
public class MerchantLoginController {

    @Autowired
    private MerchantService merchantService;

    @RequestMapping(value = "/login")
    public String login(ModelMap model, String customerNumber, String loginPassword) {

        model.put("message",ResultUtil.getResultJson(merchantService.login(customerNumber,loginPassword)));
        return "message.json";
    }
    @RequestMapping(value = "/changePassword")
    public String changePassword(ModelMap model, String customerNumber, String newPassword,String oldPassword,String session) throws SQLException {
        Assert.hasText(customerNumber,"customerNumber "+ I18nUtil.getMessage(-2,null,"en_US"));
        Assert.hasText(newPassword,"newPassword "+ I18nUtil.getMessage(-2,null,"en_US"));
        Assert.hasText(oldPassword,"oldPassword "+ I18nUtil.getMessage(-2,null,"en_US"));
        if(newPassword.equals(oldPassword)){
            throw new BaseAPIException("新旧密码不能相同！");
        }else{
            String outPutJson=ResultUtil.getResultJson(merchantService.changePassword(customerNumber,newPassword,oldPassword));

            model.put("message",outPutJson);
        }

        return "message.json";
    }
    @RequestMapping(value = "/merchantList")
    public String merchantList(ModelMap model) {
        List<Merchant> merchantList=merchantService.merchantList();
        String listJson=ResultUtil.getResultJson(merchantList);
       // model.put("message",merchantList);
        model.put("message",listJson);
//        model.put("message",ResultUtil.getResultJson(merchantService.login(customerNumber,loginPassword)));
        return "message.json";
    }

    @RequestMapping(value = "/analysis")
    public String analysis(ModelMap model,String customerNumber,String session){

        Assert.hasText(customerNumber,"customerNumber "+ I18nUtil.getMessage(-2,null,"en_US"));

        model.put("message",ResultUtil.getResultJson(merchantService.analysis(customerNumber)));
        return "message.json";
    }



    @RequestMapping(value = "/index")
    public String index() {
        return "entry";
    }

    @RequestMapping(value = "/main")
    public String main() {
        return "new_file";
    }

    @RequestMapping(value = "/revise")
    public String revise() {
        return "revise";
    }

    @RequestMapping(value = "/ff")
    public String ff() {
        return "ff";
    }
}
